Date.prototype.Format = function(fmt)   {
  var o = {   
    "M+" : this.getMonth()+1,                 // 月份
    "d+" : this.getDate(),                    // 日
    "h+" : this.getHours(),                   // 小时
    "m+" : this.getMinutes(),                 // 分
    "s+" : this.getSeconds(),                 // 秒
    "q+" : Math.floor((this.getMonth()+3)/3), // 季度
    "S"  : this.getMilliseconds()             // 毫秒
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
}
function toDate(date){
	var t = Date.parse(date);  
    if (!isNaN(t)) {  
        return new Date(Date.parse(date.replace(/-/g, "/")));  
    } else {  
        return new Date();  
    }  
}
app.controller(
	"fatherCtrl",
	function($scope, request) {
		$scope.logout = function() {
			request.getData("get", "user/outLogin", null).then(
					function(data) {
						window.location.href = "/login.html";
					});
		}
		$scope.sh = true;
		$scope.sh1 = true;
		$scope.f = true;
		$scope.f1 = true;
		$scope.f2 = false;
		$scope.f3 = false;
		$scope.show = function() {
			$scope.f = true;
			$scope.f1 = true;
			$scope.f11 = false;
			$scope.f12 = false;
			$scope.f13 = false;
			$scope.f2 = false;
			$scope.f3 = false;
			$('#sidebar li.submenu ul').slideUp();
			$(".submenu").removeClass("open");
		}
		$scope.show1 = function() {
			$scope.f = false;
			$scope.f1 = false;
			$scope.f11 = true;
			$scope.f12 = false;
			$scope.f13 = false;
			$scope.f2 = false;
			$scope.f3 = false;
		}
		$scope.show2 = function() {
			$scope.f = false;
			$scope.f1 = false;
			$scope.f11 = false;
			$scope.f12 = true;
			$scope.f13 = false;
			$scope.f2 = false;
			$scope.f3 = false;
		}
		$scope.show3 = function() {
			$scope.f = false;
			$scope.f1 = false;
			$scope.f11 = false;
			$scope.f12 = false;
			$scope.f13 = true;
			$scope.f2 = false;
			$scope.f3 = false;
		}
		$scope.show4 = function() {
			$scope.f = false;
			$scope.f1 = true;
			$scope.f11 = false;
			$scope.f12 = false;
			$scope.f13 = false;
			$scope.f2 = true;
			$scope.f3 = false;
			$('#sidebar li.submenu ul').slideUp();
			$(".submenu").removeClass("open");
		}
		$scope.show5 = function() {
			$scope.f = false;
			$scope.f1 = true;
			$scope.f11 = false;
			$scope.f12 = false;
			$scope.f13 = false;
			$scope.f2 = false;
			$scope.f3 = true;
			$('#sidebar li.submenu ul').slideUp();
			$(".submenu").removeClass("open");
		}
	})
.controller("indexController",
	function($scope, request) {
		request.getData("get", "dep/allDep", {}).then(function(data) {
			var datas=data.data;
			if (data.code == 1) {
				$scope.bdatas = datas;
			}
		});
		function users(){
			request.getData("get", "user/allUsers", {
				page : 1,
				num : 10
			}).then(function(data) {
				var datas=data.data;
				if (data.code == 1) {
					for(var i=0;i<datas.length;++i){
						datas[i].namev=datas[i].name;
						datas[i].sexv=datas[i].sex;
						datas[i].departmentv=datas[i].department;
						datas[i].addressv=datas[i].address;
						datas[i].birthv=toDate(datas[i].birth);
					}
					$scope.datas = datas;
				}
			});
		}
		users();
		$scope.addteacher = function() {
			$scope.addshow = true;
		};
		$scope.disadd = function() {
			$scope.addshow = false;
		};
		$scope.select = function() {
			var params = {};
			if ($scope.userId != null && $scope.userId != "") {
				params.userName = $scope.userId;
			}
			if ($scope.name != null && $scope.name != "") {
				params.name = $scope.name;
			}
			if ($scope.sex != null && $scope.sex != ""
					&& $scope.sex != -1) {
				params.sex = $scope.sex;
			}
			params.dep = $scope.dep;
			params.page = 1;
			params.num = 10;
			request.getData("get", "user/allUsers", params).then(
					function(data) {
						if (data.code == 1) {
							$scope.datas = data.data;
						}
					});
		};
		// 添加员工
		$scope.add = function() {
			if($scope.newUsername==null||$scope.newUsername==""){
				jin("请填写员工工号");
			}else if($scope.newName==null||$scope.newName==""){
				jin("请填写员工姓名");
			}else if($scope.newSex==null||$scope.newSex==""){
				jin("请填写员工性别");
			}else if($scope.newBirth==null||$scope.newBirth==""){
				jin("请填写员工出生日期");
			}else{
				request.getData("post", "user/checkUserName", {
					userName : $scope.newUsername
				}).then(function(data) {
					if (data.code == 1) {
						if(data.data>0){
							jin("工号已存在");
						}else{
							request.getData("post", "user/addOrUpdateUser", {
								userName : $scope.newUsername,
								name:$scope.newName,
								birth:new Date($scope.newBirth).Format("yyyy-MM-dd"),
								sex:$scope.newSex,
								dep:$scope.newDepartment,
								address:$scope.newAddress
							}).then(function(data) {
								if (data.code == 1) {
									suc("成功");
									$scope.disadd();
									users();
									$scope.newUsername="";
									$scope.newName="";
									$scope.newBirth="";
									$scope.newSex="";
									$scope.newDepartment="";
									$scope.newAddress="";
								}else{
									tip(data.msg);
								}
							});
						}
					}else{
						tip("请求失败");
					}
				});
			}
		};
		
		$scope.toupdate = function(data) {
			data.s = true;
			data.u = true;
		};
		$scope.update = function(data) {
			var sex=$("#upse").val();
			var dep=$("#updep").val();
			if(data.username==null||data.username==""){
				jin("请填写员工工号");
			}else if(data.namev==null||data.namev==""){
				jin("请填写员工姓名");
			}else if(sex==null||sex==""){
				jin("请填写员工性别");
			}else if(data.birthv==null||data.birthv==""){
				jin("请填写员工出生日期");
			}else{
				request.getData("post", "user/addOrUpdateUser", {
					userId:data.id,
					userName : data.username,
					name:data.namev,
					birth:new Date(data.birthv).Format("yyyy-MM-dd"),
					sex:sex,
					dep:dep,
					address:data.addressv
				}).then(function(data) {
					if (data.code == 1) {
						suc("成功");
						$scope.disadd();
						users();
					}else{
						tip(data.msg);
					}
				});
			}
		};
		$scope.disupdate = function(data) {
			data.s = false;
			data.u = false;
		};
		$scope.toDeletet=function(data){
			$scope.dId=data.id;
		}
		$scope.deletet = function() {
			request.getData("post", "user/deleteUser", {
				userId:$scope.dId
			}).then(function(data) {
				if (data.code == 1) {
					suc("成功");
					$scope.disadd();
					users();
				}else{
					tip(data.msg);
				}
			});
		};
		$scope.toUpdate=function(data){
			$scope.uId=data.id;
		}
		$scope.updatePwd = function() {
			request.getData("post", "user/updatePwd", {
				userId:$scope.uId,
				pwd:$scope.uPwd
			}).then(function(data) {
				if (data.code == 1) {
					suc("成功");
					$scope.disadd();
					users();
				}else{
					tip(data.msg);
				}
			});
		};
	})
.controller("depController",
	function($scope, request) {
		function users(){
			request.getData("get", "dep/allDep", {}).then(function(data) {
				var datas=data.data;
				if (data.code == 1) {
					for(var i=0;i<datas.length;++i){
						datas[i].namev=datas[i].name;
						datas[i].createtime=new Date(datas[i].createtime).Format("yyyy-MM-dd hh:mm:ss");
						datas[i].infov=datas[i].info;
					}
					$scope.datas = datas;
				}
			});
		}
		users();
		$scope.addteacher = function() {
			$scope.addshow = true;
		};
		$scope.disadd = function() {
			$scope.addshow = false;
		};
		// 添加
		$scope.add = function() {
			if($scope.newName==null||$scope.newName==""){
				jin("请填写部门名称");
			}else{
				request.getData("post", "dep/addOrUpdateDep", {
					name:$scope.newName,
					info:$scope.newInfo
				}).then(function(data) {
					if (data.code == 1) {
						suc("成功");
						$scope.disadd();
						users();
						$scope.newName="";
						$scope.newInfo="";
					}else{
						tip(data.msg);
					}
				});
			}
		};
		
		$scope.toupdate = function(data) {
			data.s = true;
			data.u = true;
		};
		$scope.update = function(data) {
			if(data.namev==null||data.namev==""){
				jin("请填写部门名称");
			}else{
				request.getData("post", "dep/addOrUpdateDep", {
					depId:data.id,
					name:data.namev,
					info:data.infov
				}).then(function(data) {
					if (data.code == 1) {
						suc("成功");
						$scope.disadd();
						users();
					}else{
						tip(data.msg);
					}
				});
			}
		};
		$scope.disupdate = function(data) {
			data.s = false;
			data.u = false;
		};
		$scope.toDeletet=function(data){
			$scope.dId=data.id;
		}
		$scope.deletet = function() {
			request.getData("post", "dep/deleteDep", {
				depId:$scope.dId
			}).then(function(data) {
				if (data.code == 1) {
					suc("成功");
					$scope.disadd();
					users();
				}else{
					tip(data.msg);
				}
			});
		};
	})
.controller("adminController",
	function($scope, request) {
		request.getData("get", "cha/all", {}).then(function(data) {
			var datas=data.data;
			if (data.code == 1) {
				$scope.bdatas = datas;
			}
		});
		function users(){
			request.getData("get", "user/allAdmins", {}).then(function(data) {
				var datas=data.data;
				if (data.code == 1) {
					for(var i=0;i<datas.length;++i){
						datas[i].usernamev=datas[i].username;
						datas[i].createtime=new Date(datas[i].createtime).Format("yyyy-MM-dd hh:mm:ss");
					}
					$scope.datas = datas;
				}
			});
		}
		users();
		$scope.addteacher = function() {
			$scope.addshow = true;
		};
		$scope.disadd = function() {
			$scope.addshow = false;
		};
		// 添加
		$scope.add = function() {
			if($scope.newUserName==null||$scope.newUserName==""){
				jin("请填写账号");
			}else if($scope.newPwd==null||$scope.newPwd==""){
				jin("请填写密码");
			}else if($scope.newPwd.length<6){
				jin("密码格式不正确");
			}else{
				request.getData("post", "user/checkUserName", {
					userName : $scope.newUserName
				}).then(function(data) {
					if (data.code == 1) {
						if(data.data>0){
							jin("账号已存在");
						}else{
							request.getData("post", "user/addOrUpdateAdmin", {
								userName : $scope.newUserName,
								pwd:$scope.newPwd,
								characterId:$scope.newCharacterid
							}).then(function(data) {
								if (data.code == 1) {
									suc("成功");
									$scope.disadd();
									users();
									$scope.newUserName="";
									$scope.newPwd="";
								}else{
									tip(data.msg);
								}
							});
						}
					}else{
						tip("请求失败");
					}
				});
			}
		};
		
		$scope.toupdate = function(data) {
			data.s = true;
			data.u = true;
		};
		$scope.update = function(data) {
			var characterid=$("#upse").val();
			if(data.usernamev==null||data.usernamev==""){
				jin("请填写账号");
			}else{
				request.getData("post", "user/addOrUpdateAdmin", {
					userId:data.id,
					userName : data.usernamev,
					characterId:characterid
				}).then(function(data) {
					if (data.code == 1) {
						suc("成功");
						$scope.disadd();
						users();
					}else{
						tip(data.msg);
					}
				});
			}
		};
		$scope.disupdate = function(data) {
			data.s = false;
			data.u = false;
		};
		$scope.toDeletet=function(data){
			$scope.dId=data.id;
		}
		$scope.deletet = function() {
			request.getData("post", "user/deleteAdmin", {
				userId:$scope.dId
			}).then(function(data) {
				if (data.code == 1) {
					suc("成功");
					$scope.disadd();
					users();
				}else{
					tip(data.msg);
				}
			});
		};
		$scope.toUpdate=function(data){
			$scope.uId=data.id;
		}
		$scope.updatePwd = function() {
			request.getData("post", "user/updateAdminPwd", {
				userId:$scope.uId,
				pwd:$scope.uPwd
			}).then(function(data) {
				if (data.code == 1) {
					suc("成功");
					$scope.disadd();
					users();
				}else{
					tip(data.msg);
				}
			});
		};
	})
.controller("roleController",
	function($scope, request) {
		$scope.toUpdate=function(data){
			request.getData("get", "module/all", {chaId:data.id}).then(function(data) {
				var datas=data.data;
				if (data.code == 1) {
					$scope.bdatas = datas;
				}
			});
			$scope.sId=data.id;
		}
		function users(){
			request.getData("get", "cha/all", {}).then(function(data) {
				var datas=data.data;
				if (data.code == 1) {
					for(var i=0;i<datas.length;++i){
						datas[i].namev=datas[i].name;
						datas[i].createtime=new Date(datas[i].createtime).Format("yyyy-MM-dd hh:mm:ss");
						datas[i].infov=datas[i].info;
					}
					$scope.datas = datas;
				}
			});
		}
		users();
		$scope.addteacher = function() {
			$scope.addshow = true;
		};
		$scope.disadd = function() {
			$scope.addshow = false;
		};
		// 添加
		$scope.add = function() {
			if($scope.newName==null||$scope.newName==""){
				jin("请填写角色名称");
			}else{
				request.getData("post", "cha/addOrUpdate", {
					name:$scope.newName,
					info:$scope.newInfo
				}).then(function(data) {
					if (data.code == 1) {
						suc("成功");
						$scope.disadd();
						users();
						$scope.newName="";
						$scope.newInfo="";
					}else{
						tip(data.msg);
					}
				});
			}
		};
		
		$scope.toupdate = function(data) {
			data.s = true;
			data.u = true;
		};
		$scope.update = function(data) {
			if(data.namev==null||data.namev==""){
				jin("请填写角色名称");
			}else{
				request.getData("post", "cha/addOrUpdate", {
					chaId:data.id,
					name:data.namev,
					info:data.infov
				}).then(function(data) {
					if (data.code == 1) {
						suc("成功");
						$scope.disadd();
						users();
					}else{
						tip(data.msg);
					}
				});
			}
		};
		$scope.disupdate = function(data) {
			data.s = false;
			data.u = false;
		};
		$scope.toDeletet=function(data){
			$scope.dId=data.id;
		}
		$scope.deletet = function() {
			request.getData("post", "cha/delete", {
				chaId:$scope.dId
			}).then(function(data) {
				if (data.code == 1) {
					suc("成功");
					$scope.disadd();
					users();
				}else{
					tip(data.msg);
				}
			});
		};
		$scope.changeModules = function() {
			var checkValues=[]
			$.each($('input:checkbox:checked'),function(){
				checkValues.push($(this).val());
            });
			request.getData("post", "cha/setModules", {
				chaId:$scope.sId,
				modules:checkValues
			}).then(function(data) {
				if (data.code == 1) {
					suc("成功");
					$scope.disadd();
					users();
				}else{
					tip(data.msg);
				}
			});
		};
		
	})
	.controller("moduleController",
			function($scope, request) {
		function users(){
			request.getData("get", "module/all", {}).then(function(data) {
				var datas=data.data;
				if (data.code == 1) {
					for(var i=0;i<datas.length;++i){
						datas[i].namev=datas[i].name;
						datas[i].pathv=datas[i].path;
						datas[i].createtime=new Date(datas[i].createtime).Format("yyyy-MM-dd hh:mm:ss");
						datas[i].infov=datas[i].info;
					}
					$scope.datas = datas;
				}
			});
		}
		users();
		$scope.addteacher = function() {
			$scope.addshow = true;
		};
		$scope.disadd = function() {
			$scope.addshow = false;
		};
		// 添加
		$scope.add = function() {
			if($scope.newName==null||$scope.newName==""){
				jin("请填写权限名称");
			}else if($scope.newPath==null||$scope.newPath==""){
				jin("请填写权限路径");
			}else{
				request.getData("post", "module/addOrUpdate", {
					name:$scope.newName,
					path:$scope.newPath,
					info:$scope.newInfo
				}).then(function(data) {
					if (data.code == 1) {
						suc("成功");
						$scope.disadd();
						users();
						$scope.newName="";
						$scope.newPath="";
						$scope.newInfo="";
					}else{
						tip(data.msg);
					}
				});
			}
		};
		
		$scope.toupdate = function(data) {
			data.s = true;
			data.u = true;
		};
		$scope.update = function(data) {
			if(data.namev==null||data.namev==""){
				jin("请填写角色名称");
			}else if(data.pathv==null||data.pathv==""){
				jin("请填写权限路径");
			}else{
				request.getData("post", "module/addOrUpdate", {
					id:data.id,
					name:data.namev,
					path:data.pathv,
					info:data.infov
				}).then(function(data) {
					if (data.code == 1) {
						suc("成功");
						$scope.disadd();
						users();
					}else{
						tip(data.msg);
					}
				});
			}
		};
		$scope.disupdate = function(data) {
			data.s = false;
			data.u = false;
		};
		$scope.toDeletet=function(data){
			$scope.dId=data.id;
		}
		$scope.deletet = function() {
			request.getData("post", "module/delete", {
				id:$scope.dId
			}).then(function(data) {
				if (data.code == 1) {
					suc("成功");
					$scope.disadd();
					users();
				}else{
					tip(data.msg);
				}
			});
		};
	})
	.controller("passwordController",
			function($scope, request) {
		$scope.upd = function() {
			if($scope.pwd1==$scope.pwd2){
				jin("新密码与原密码相同");
			}else if ($scope.pwd2!=$scope.pwd3){
				jin("密码两次输入不相同");
			}else{
				request.getData("post", "user/updateMyPwd", {
					oldPwd:$scope.pwd1,
					pwd:$scope.pwd2
				}).then(function(data) {
					if (data.code == 1) {
						suc("成功");
					}else{
						tip(data.msg);
					}
				});
			}
		};
	})